import BaseMod from "../core/mvc/BaseMod";
import { ISize } from "../../common/game/util/V2Util";
import { GameEvent } from "../../common/game/event/GameEvent";
import App from "../App";
import { Protocol } from "../../common/protocol/Protocol";
import { ClientEvent } from "../event/ClinetEvent";

const cet = ClientEvent.Type
const get = GameEvent.Type

/** 世界数据 */
export default class WorldMod extends BaseMod {

    private static _ins: WorldMod
    static get ins() { return WorldMod._ins || (WorldMod._ins = new WorldMod()) }

    /** 世界大小 */
    private _worldSize: ISize = { w: 888, h: 888 }

    /** 世界大小 */
    get worldSize() { return this._worldSize }

    protected initLocal(): void {
        this.updateWorldSize(App.localGame.world.size)

        this.gd.on(get.GAME_UPDATE, this, e => {
            let msgs = e.game.world.extractMsgs()
            msgs.length && this.ed.send(cet.MSGS, { msgs })
        })
    }

    protected initRemote(): void {
        this.pd.on(Protocol.S2C_WORLD_SIZE, this, data => {
            this.updateWorldSize(data)
        })
    }

    private updateWorldSize(size: ISize) {
        this._worldSize = size
        this.ed.send(cet.WORLD_SIZE_UPDATE, { size })
    }

}